package com.wangshuile.netty.ch1;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	private ServerSocket serverSocket;
	
	public Server(int port) {
		try{
			this.serverSocket=new ServerSocket(port);
			System.out.println("服务器启动成功，端口"+port);
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("服务器启动失败");
		}
	}
	
	public void start() {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				doStart();
			}

		}).start();
	}
	
	private void doStart() {
		// TODO Auto-generated method stub
		while(true) {
			try {
				Socket client=serverSocket.accept();
				new ClientHandler(client).start();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("服务器异常");
			}
		}
	}
}
